package com.xtzn.business.impl;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xtzn.business.ITaskListBusiness;
import com.xtzn.click.vo.CSResponse;
import com.xtzn.common.Constant;
import com.xtzn.core.enums.ResponseEnum;
import com.xtzn.mapper.entity.TaskList;
import com.xtzn.service.ITaskListService;
import com.xtzn.utils.ListUtil;
import com.xtzn.utils.MinuteUtil;
import com.xtzn.utils.RedisUtil;
import com.xtzn.vo.TaskRequest;

/**
 * 功能描述: <br>
 * **
 * 
 * @author zyw
 * @version [版本号, 2018年4月18日]
 */
@Component
public class TaskListBusinessImpl implements ITaskListBusiness {

	//private static final Logger log = LoggerFactory.getLogger(TaskListBusinessImpl.class);
	@Autowired
	private ITaskListService taskListService;
	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年4月18日]
	 */
	@Override
	public CSResponse retrieveTaskListByPage(TaskRequest taskRequest) {
		Integer pageSize = Constant.pageSize;
		Integer skips = (taskRequest.getPageIndex()-1)*pageSize;
		taskRequest.setPageSize(pageSize);
		taskRequest.setSkips(skips);
		List<TaskList> list = taskListService.selectTaskListByPage(taskRequest);
		if(!list.isEmpty()){
			Map<String,Object> map = new HashMap<String, Object>();	
			Integer count  = taskListService.selectCount(taskRequest);
			map.put("data", list);
			map.put("count", count);
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc(),(Serializable)map);
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年4月18日]
	 */
	@Override
	public CSResponse retrieveTaskListById(Integer id) {
		TaskList taskList = taskListService.selectTaskListById(id);
		if(null!=taskList){
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc(),(Serializable)taskList);
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年4月18日]
	 */
	@Override
	public CSResponse deleteById(Integer id) {
		int i = taskListService.deleteById(id);
		if(i>0){
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc());
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年4月18日]
	 */
	@Override
	public CSResponse deleteBatchById(List<Integer> list) {
		int i = taskListService.deleteBatchById(list);
		if(i>0){
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc());
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年4月18日]
	 */
	@Override
	public CSResponse insertTaskList(TaskList taskList) {
		//根据目标数和时间轴方案计算出具体方案详情
		Integer targetNum = taskList.getTargetNum();
		String timeline = taskList.getTimeline();
		String[] timelines = timeline.split(",");
		String timelinedtl = "";
		double percent = 0;
		int number = 0;
		for(int i = 0;i < timelines.length;i++){
			percent = Double.parseDouble(timelines[i]);
			number = (int)Math.ceil(percent*targetNum/100);
			timelinedtl = timelinedtl+MinuteUtil.getMinuteRandomValue(number)+";";
		}
		timelinedtl = timelinedtl.substring(0, timelinedtl.length()-1);
		taskList.setTimelinedtl(timelinedtl);
		//计算cr值
		if(taskList.getCrMin()!=null&&taskList.getCrMax()!=null){
			double crMin = taskList.getCrMin();
			double crMax = taskList.getCrMax();
			double crValue = crMin+Math.random()*(crMax-crMin);
			DecimalFormat df = new DecimalFormat("0.0");
			crValue = Double.parseDouble(df.format(crValue));
			taskList.setCrValue(crValue);
		}		
		int i = taskListService.insertTaskList(taskList);
		if(i>0){
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc());
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年4月18日]
	 */
	@Override
	public CSResponse updateTaskList(TaskList taskList) {
		int i = taskListService.updateTaskList(taskList);
		if(i>0){
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc());
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年4月26日]
	 */
	@Override
	public CSResponse updateBatchById(List<Integer> list) {
		int i = taskListService.updateBatchById(list);
		if(i>0){
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc());
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年5月10日]
	 */
	@Override
	public CSResponse updateLinkState(Byte linkState, Integer userId) {
		int i = taskListService.updateLinkState(linkState, userId);
		if(i>0){
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc());
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年5月10日]
	 */
	@Override
	public CSResponse updateTaskListCPI(TaskList taskList, String ids) {
		List<Integer> list = ListUtil.dataList(ids);
		if(taskList.getCrMin()!=null&&taskList.getCrMax()!=null){
			double crMin = taskList.getCrMin();
			double crMax = taskList.getCrMax();
			double crValue = crMin+Math.random()*(crMax-crMin);
			DecimalFormat df = new DecimalFormat("0.0");
			crValue = Double.parseDouble(df.format(crValue));
			taskList.setCrValue(crValue);
		}
		int i = taskListService.updateTaskListCPI(list, taskList.getWaitingForNum(), taskList.getCrMin(), 
				taskList.getCrMax(), taskList.getCrValue(),taskList.getPlayTimeMin(), taskList.getPlayTimeMax());
		
		if(i>0){
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc());
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年5月11日]
	 */
	@Override
	public CSResponse homePage(Integer userId) {
		Map<String,Object> map = new HashMap<>();
		String yestodayTaskTotal = RedisUtil.hget("taskTargetNum", String.valueOf(userId));
		String yestodayFinishNum = RedisUtil.hget("taskFinishNum", String.valueOf(userId));
		String yestodayUndoRetained = RedisUtil.hget("retained", String.valueOf(userId));
		if(null==yestodayTaskTotal){
			yestodayTaskTotal = "0";
		}
		if(null==yestodayFinishNum){
			yestodayFinishNum = "0";
		}
		if(null==yestodayUndoRetained){
			yestodayUndoRetained = "0";
		}
		map.put("yestodayTaskTotal", yestodayTaskTotal);
		map.put("yestodayFinishNum", yestodayFinishNum);
		map.put("yestodayUndoRetained", yestodayUndoRetained);
		return new CSResponse((Serializable)map);
	}

}
